/***************************************************************************
 *   Copyright (C) 2007 by Jiří Němec   *
 *   nemec.jiri@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QRegExp>
#include <QDir>
#include <QVector>
#include "hledani.h"

Hledani::Hledani(HashTabulka * primary, QString confDir) {
    this->slovnik = primary;
    this->localFileName = confDir+QDir::separator() + "slovnik.zcu";

}

Hledani::~Hledani() {
    this->dataFile.close();
}

void Hledani::run() {
    //qDebug("poustim vlakno hledani");
    startHledani(this->text);
}

void Hledani::setText(const QString &text) {
    this->text = text;
}
void Hledani::setPriority(const QString &priority) {
    this->priority = priority;
}

void Hledani::setStop() {
    stop = true;
    //emit vysledekHledani(text);
}

struct LessThan {
    QString vzor;
    QRegExp regexp;
    bool priorityEn;
    LessThan(QString vzor,bool priorityEn) {
        this->priorityEn = priorityEn;
        regexp.setPattern("^" + vzor);
        regexp.setCaseSensitivity(Qt::CaseInsensitive);
    }
    bool operator()(const Radek &r1, const Radek &r2) {
        if (this->priorityEn) {
            if (r1.anglicky == r2.anglicky) return r1.cesky.toLower() < r2.cesky.toLower();
            if (r1.anglicky.contains(this->regexp) && !r2.anglicky.contains(this->regexp)) return true;
            if (r2.anglicky.contains(this->regexp) && !r1.anglicky.contains(this->regexp)) return false;
            else return r1.anglicky.toLower() < r2.anglicky.toLower();
        } else {
            if (r1.cesky == r2.cesky) return r1.anglicky.toLower() < r2.anglicky.toLower();
            if (r1.cesky.contains(this->regexp) && !r2.cesky.contains(this->regexp)) return true;
            if (r2.cesky.contains(this->regexp) && !r1.cesky.contains(this->regexp)) return false;
            else return r1.cesky.toLower() < r2.cesky.toLower();
        }
        return false;
    }
};

void Hledani::startHledani(const QString &text) {
    this->stop = false;
    if (text.length() > 1) {

        if (!this->dataFile.isOpen() ) {
            this->dataFile.setFileName(localFileName);
            if ( !this->dataFile.open (QIODevice::ReadOnly | QIODevice::Text) ) {
                emit zmenaStatusu ( "Nejde cist z disku" );
            }
        }
        QString ret;
        QVector<qint64> findV = this->slovnik->najdi(text);
        QVector<Radek> retV;
        retV.reserve(findV.size());
        QRegExp regExp("^([^\t#]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\n]*)");
        QRegExp regExpText("^" + this->text);
        for (QVector<qint64>::const_iterator it = findV.constBegin();!this->stop && it != findV.constEnd();++it ) {
            if (this->stop) return;
            this->dataFile.seek(*it);
            if (this->stop) return;
            if (regExp.indexIn(QString::fromUtf8(this->dataFile.readLine())) > -1) {
                if (regExp.cap(1).contains(regExpText) || regExp.cap(2).contains(regExpText) )
                    retV.push_back(Radek(regExp.cap(1),regExp.cap(2),regExp.cap(3),regExp.cap(4),regExp.cap(5),*it ));
            }
        }
        LessThan a(text,(this->priority == "en"));
        qSort(retV.begin(), retV.end(), a );
        if (!this->stop) emit vysledekHledani(retV);
    }
}

Radek Hledani::najdiPodlePozice(qint64 pos) {
    Radek ret;
    if (!this->dataFile.isOpen() ) {
        this->dataFile.setFileName(localFileName);
        if ( !this->dataFile.open (QIODevice::ReadOnly | QIODevice::Text) ) {
            emit zmenaStatusu ( "Nejde cist z disku" );
        }
    }
    QRegExp regExp("^([^\t#]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\n]*)");
    this->dataFile.seek(pos);
    if (regExp.indexIn(QString::fromUtf8(this->dataFile.readLine())) > -1)
        ret = Radek(regExp.cap(1),regExp.cap(2),regExp.cap(3),regExp.cap(4),regExp.cap(5),pos );
    return ret;
}

